var DIC = (function(dic){
		//基本路径
		dic.contextPath = function getPath(){
			var pathName = window.location.pathname; 
			if(pathName.length == 1){
				return "";
			}
	     	var webName =  pathName.substring(0, pathName.indexOf('/',1));   
	     	return webName;
		}();
		dic.cacheMap = {};
		/**
		 * 字典的key的前缀
		 * @param {Object} dics
		 * @return {TypeName} 
		 */
		dic.prefix = "treeOrdic_";
		dic.typeJoinParent = "_dicParantJoin_";
		
		dic.treeCache = {};
		dic.NULL = "NULL";
		function defaultFilter(dics){
			var resultDics = [];
			for(var i = 0; i < dics.length; i++){
				if(dics[i].disabled == 0){
					resultDics.push(dics[i]);
				}
			}
			return resultDics;
		}
		
		function getOption(value, text, isSelected){
			if(isSelected){
				return "<option selected='selected' value='" + value + "'>" + text + "</option>";
			}else{
				return "<option value='" + value + "'>" + text + "</option>";
			}
		}
		/**
		 * 加载字典缓存 (注意这个不要加载 大型的树形字典，原因是树形字典一般很大)
		 * @param {Object} types
		 * @param {Object} key
		 * @return {TypeName} 
		 */
		dic.loadTreeTypeAndParent = function (type, parent){
			if(parent == null){
				parent = dic.NULL;
			}
			if(dic.treeCache[type + dic.typeJoinParant + parent]){
				return dic.treeCache[type + dic.typeJoinParant + parent];
			}
			var result = [];
			$.ajax({
				url : dic.contextPath + "/toDictionaryService.dic",
				type : "POST",
				async : false,
				data : {
					action : 'getDicChildren',
					type : type,
					parent: parent
				},
				traditional : true,
				cache : false,
				dataType : 'json',
				success : function(data) {
					if(!parent){
						parent = "null";
					}
					if(data.success){
						if(data.msg){
							dic.treeCache[type + dic.typeJoinParent + parent] = data.msg
							result = data.msg;
						}
					}else{
						alert(data.msg);
					}
				}
			});
			return result;
		}
		
		/**
		 * 加载字典缓存 (注意这个不要加载 大型的树形字典，原因是树形字典一般很大)
		 * @param {Object} types
		 * @param {Object} key
		 * @return {TypeName} 
		 */
		dic.loadTypes = function (types){
			$.ajax({
				url : dic.contextPath + "/toDictionaryService.dic",
				type : "POST",
				async : false,
				data : {
					action : 'getDicAll',
					types : types,
					filter: true
				},
				traditional : true,
				cache : false,
				dataType : 'json',
				success : function(data) {
					if(data.success){
						if(data.msg){
							for(var pro in data.msg){
								dic.cacheMap[dic.prefix + pro] = data.msg[pro];
							}
						}
					}else{
						alert(data.msg);
					}
				}
			});
		}
		/**
		 * 获取一个字典
		 * @param {Object} type
		 * @param {Object} key
		 * @return {TypeName} 
		 */
		dic.getDicByKey = function(type, key, filter){
			var dicList = [];
			if(dic.cacheMap[dic.prefix + type]){
				dicList = dic.cacheMap[dic.prefix + type];
			}else{
				dic.loadTypes([type]);
				dicList = dic.cacheMap[dic.prefix + type];
				if(!dicList){
					return;
				}
			}
			if(filter){
				dicList = defaultFilter(dicList);
			}
			var result = null;
			for(var i = 0; i < dicList.length; i++){
				if(dicList[i].key == key){
					result = dicList[i];
					break;
				}
			}
			return result;
		}
		/**
		 * 获取一个字典值
		 * @param {Object} type
		 * @param {Object} key
		 * @return {TypeName} 
		 */
		dic.getDicStringByKey = function(type, key, defaultValue, filter){
			if(!defaultValue){
				defaultValue = "";
			}
			var tempDic = dic.getDicByKey(type, key, filter);
			if(tempDic && tempDic.value){
				return tempDic.value;
			}else{
				return defaultValue;
			}
		}
		
		dic.getDicList = function(type, dontfilter){
			var dicList = null;
			if(dic.cacheMap[dic.prefix + type]){
				dicList = dic.cacheMap[dic.prefix + type];
			}else{
				dic.loadTypes([type]);
				dicList = dic.cacheMap[dic.prefix + type];
			}
			
			dicList = dicList ? dicList : [];
			
			if(!dontfilter){
				dicList = defaultFilter(dicList);
			}
			return dicList;
		}
		
		/**
		 * 设置所有的select
		 * @param {Object} sels[{
		 * $select
		 * defaultkey
		 * parent
		 * }]
		 */
		dic.setSelect = function(type, sels){
			
			var length = sels.length;
			var parent = sels[0].parent;
			if(!parent || parent == "" || parent == null){
				parent = "-";
			}
			
			for(var j = 0; j < (length - 1); j++){
				(function(){
					var sel = sels[j];
					sel.selNext = sels[j + 1]
					var next = sels[j + 1]
					sel.$select.change(function (){
						selectFill(type, sel.selNext, sel.$select.val());
					})
				})();
			}
			selectFill(type, sels[0], parent);
		}
		function selectFill(type, sel, parent){
			if(!parent|| parent == ""){
				sel.$select.html(sel.defaultOption);
				sel.$select.change();
				return;
			}
			if(parent == "-"){
				parent = null;
			}
			var dics = dic.loadTreeTypeAndParent(type, parent);
				if(!sel.defaultOption){
					sel.defaultOption = sel.$select.html();
				}
				var option = "" + sel.defaultOption;
				for(var i = 0; i < dics.length; i++){
					option = option + getOption(dics[i].key, dics[i].value, dics[i].key == dics[i].defaultkey)
				}
				sel.$select.html(option);
		}
		return dic;
})({});